package graphicinterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Book;
import bookworm.BooksAndRatingOfOneAuthor;

/**
 * This class will show a window to the user with the best authors for a specified tag.
 * It'll also show how good was the author about the tag(average rating) and how many
 * books each author wrote about the specified tag. When a user selects an author, this window
 * will redirect the user to another window with books from the author.
 * @author Mina
 *
 */
public class WindowBestAuthorsBasedOnTag implements MouseListener
{
	private String tag;
	private LinkedList<BooksAndRatingOfOneAuthor> bestAuthorsAndBooksBasedOnTag;
	private JFrame windowBestAuthorsBasedOnTag;
	private LinkedList<JPanel> panelsWithAuthorsAndRatings;
	
	/**
	 * this panel will show author names and related ratings for each author based
	 * on the tag defined in "tag"
	 */
	private ShowsJPanelsOnManyPages showAuthorsAndRatings;
	 
	
	public WindowBestAuthorsBasedOnTag(String tag)
	{
		this.tag = tag;
		SystemFunctionalitiesFacade knowsAllSystemFunctionalities = 
											SystemFunctionalitiesFacade.getInstance();
		this.bestAuthorsAndBooksBasedOnTag =
				knowsAllSystemFunctionalities.getBestAuthorsAndBooksBasedOnTag(tag);
		this.windowBestAuthorsBasedOnTag = new JFrameWithMenu("Best authors based on tag " + tag);
		
		this.createPanelsWithAuthorsAndRatings();
		this.showAuthorsAndRatings =
				new ShowsJPanelsOnManyPages(panelsWithAuthorsAndRatings, 4, 4);
		
		this.drawInitialPageWithAuthorsAndRatings();
		
	}
	
	private void createPanelsWithAuthorsAndRatings()
	{
		this.panelsWithAuthorsAndRatings = new LinkedList<JPanel>();
		for(int i = 0; i < this.bestAuthorsAndBooksBasedOnTag.size(); i++)
		{
			BooksAndRatingOfOneAuthor booksAndRatingOfOneAuthor = 
					this.bestAuthorsAndBooksBasedOnTag.get(i);
			JPanel panelAuthorAndRating =
					createJPanelWithAuthorAndRating(booksAndRatingOfOneAuthor);
			this.panelsWithAuthorsAndRatings.add(panelAuthorAndRating);
			panelAuthorAndRating.addMouseListener(this);
		}
	}
	
	private void drawInitialPageWithAuthorsAndRatings()
	{
		JPanelWithBackgroundImage panelBestAuthorsBasedOnTag =
				new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		JLabel labelWithWindowTitle = 
				TextLabelCreator.createStyledTextlabel("best authors for tag " + this.tag, 20);
		JPanel panelWithWindowTitle = new JPanel();
		panelWithWindowTitle.setBackground(new Color(255,131,137));
		panelWithWindowTitle.add(labelWithWindowTitle);
		
		GridBagLayout layout = new GridBagLayout();
		panelBestAuthorsBasedOnTag.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridheight = 1;
		grid.gridy = 0;
		
		panelBestAuthorsBasedOnTag.add(panelWithWindowTitle, grid);
		
		
		/*now let's add that panel that will show authors and ratings on many pages*/
		
		grid.gridheight = 6;
		grid.gridy = 1;
		grid.weighty = 0.3;
		panelBestAuthorsBasedOnTag.add(showAuthorsAndRatings, grid);
		
		this.windowBestAuthorsBasedOnTag.add(panelBestAuthorsBasedOnTag);
		this.windowBestAuthorsBasedOnTag.setVisible(true);
	}
	
	private JPanel createJPanelWithAuthorAndRating
								(BooksAndRatingOfOneAuthor booksAndRatingOfOneAuthor)
	{
		String author = booksAndRatingOfOneAuthor.getAuthor();
		int averageRatingBasedOnTag = 
				booksAndRatingOfOneAuthor.getAverageRatingOfAuthorBasedOnTag();
		LinkedList<Book> booksFromAuthorRelatedToTag = 
				booksAndRatingOfOneAuthor.getBooksFromAuthorRelatedToTag();
		int howManyBooksFromTagDidAuthorWrite = booksFromAuthorRelatedToTag.size();
		JLabel labelAuthor = TextLabelCreator.createStyledTextlabel(author);
		JLabel labelWithStars =
				CreatesLabelWith5StarRating.createLabelWithStars(averageRatingBasedOnTag);
		JLabel labelBooksCreatedByAuthor =
				TextLabelCreator.createStyledTextlabel("wrote " +
							howManyBooksFromTagDidAuthorWrite + " books from specified tag");
		
		//now let's add it all to one JPanel
		JPanel panelWithAuthorAndRating = new JPanel();
		panelWithAuthorAndRating.setBackground(new Color(255,131,137));
		GridBagLayout layout = new GridBagLayout();
		panelWithAuthorAndRating.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridy = 0;
		grid.gridx = 0;
		grid.gridwidth = 1;
		panelWithAuthorAndRating.add(labelAuthor, grid);

		grid.gridx = 1;
		panelWithAuthorAndRating.add(labelWithStars, grid);
		
		grid.gridx = 0;
		grid.gridy = 1;
		grid.gridwidth = 2;
		panelWithAuthorAndRating.add(labelBooksCreatedByAuthor, grid);
		
		return panelWithAuthorAndRating;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
	}

	@Override
	public void mouseEntered(MouseEvent arg0) 
	{
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) 
	{
		Component objectClicked = arg0.getComponent();
		for(int i = 0; i < this.panelsWithAuthorsAndRatings.size(); i++)
		{
			JPanel panelWithAuthorAndRating = this.panelsWithAuthorsAndRatings.get(i);
			if(objectClicked == panelWithAuthorAndRating)
			{
				//user clicked on the name of one of the authors
				int indexOfObjectClicked = i;
				/*this index is the same from the LinkedList "bestAuthorsAndBooksBasedOnTag".
				  So, we'll create a new page to show the books from the selected author
				  based on this index*/
				BooksAndRatingOfOneAuthor booksAndRatingOfSelectedAuthor =
						this.bestAuthorsAndBooksBasedOnTag.get(indexOfObjectClicked);
				WindowBooksFromAuthor windowBooksFromAuthor =
						new WindowBooksFromAuthor(booksAndRatingOfSelectedAuthor);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


}
